<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\AdminLog;
use app\admin\model\AuthGroupAccessZoom;
use fast\Random;
use think\Cache;
use think\captcha\Captcha;
use think\Cookie;
use think\Db;
use think\db\Query;
use think\Exception;
use think\Log;
use app\common\controller\Backend;
use think\Config;
use think\Hook;
use think\Session;
use think\Validate;
use app\common\library\Sms;
use fast\Http;
use app\common\controller\Callfunc;
//use addons\txim\TLSSigAPI;
//use app\admin\controller\userkanban\Customer;
use think\Request;
use app\admin\library\Auth;


/**
 * 后台首页
 * @internal
 */
class Index extends Backend
{

    protected $noNeedLogin = ['home', 'login', 'register', 'sendMsm', 'findpwd', 'about', 'loginold', 'changelanguage', 'page', 'getCaptcha'];
    protected $noNeedRight = ['index', 'logout', 'changelanguage'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();

        $this->view->assign('isLogin', $this->auth->isLogin());
    }

    /**
     * 后台首页
     */
    public function index()
    {
//--------------------------------------------------------------------

        //左侧菜单
        list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
            'dashboard' => 'hot',
            'addon'     => ['new', 'red', 'badge'],
            'auth/rule' => __('Menu'),
            'general'   => ['new', 'purple'],
        ], $this->view->site['fixedpage']);

        $action = $this->request->request('action');
        if ($this->request->isPost()) {
            if ($action == 'refreshmenu') {
                $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
            }
        }
        // 是否关联云通信
//        if (Config::get('apiSwitches.im')){
//            // 获取TLS云通信的登录签名sig
//            $TLSapi = new TLSSigAPI();
//            $userSig = $TLSapi->isTLS($this->auth->account_id);
//            if (!$userSig){
//                $userSig = null;
//            }
//        }else{
            $userSig = null;
       // }
        $this->view->assign('userSign', $userSig);
        $this->view->assign('menulist', $menulist);
        $this->view->assign('navlist', $navlist);
        $this->view->assign('fixedmenu', $fixedmenu);
        $this->view->assign('referermenu', $referermenu);
        $this->view->assign('title', __('Home'));

        // 查询当前账号对应角色的类型
        $group_info = Db::name('auth_group_access_zoom')->alias('aga')
            ->join('auth_group_zoom ag', 'aga.group_id = ag.id')
            ->where('aga.uid', '=', $this->auth->id)
            ->field('aga.group_id, ag.type')
            ->find();
        $group_id   = $group_info['group_id'];
        $group_type = $group_info['type'];
        $open_im = in_array($group_type, [3]) ? true : false;

        $this->view->assign('open_im', $open_im);
        $this->view->assign('group_id', $group_id);
        $this->view->assign('group_type', $group_type);
        $this->view->assign('username', config('imPrefix').$this->auth->username);
        $this->view->assign('nickname', $this->auth->nickname);
        $this->view->assign('avatar', config("resourceServer").$this->auth->avatar);
        return $this->view->fetch();
    }

    /**
     * 用户与管理员登录
     */
    public function login()
    {
        $url = $this->request->get('url', 'index/index');
        // $url = substr($url,0,strpos($url, '?')).'?ref=addtabs';
        if ($this->auth->isLogin()) {
            $this->success(__("You've logged in, do not login again"), $url, ['url'=>$url]);
        }
        if ($this->request->isPost()) {
            $username = $this->request->post('username');
            $password = $this->request->post('password');
            $keeplogin = $this->request->post('keeplogin');
            $token = $this->request->post('__token__');
            $go_type = $this->request->post('go_type');

            $rule = [
                'username'  => 'require|length:3,20',
                'password'  => 'require|length:3,30',
                //'__token__' => 'token',
            ];
            $data = [
                'username'  => $username,
                'password'  => $password,
                //'__token__' => $token,
            ];
            $msg = [
                'username.require'   => __('Account cannot be empty'),
                'username.length'    => __('Account must be 3-20 characters'),
            ];

            // 登录是否开启图片验证码
            if (Config::get('fastadmin.login_captcha')) {
                $rule['captcha'] = 'require|captcha';
                $data['captcha'] = $this->request->post('captcha');
                $validate = new Validate($rule, $msg, ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
            }else{
                $validate = new Validate($rule, $msg, ['username' => __('Username'), 'password' => __('Password')]);
            }
            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()), $url, ['token' => $this->request->token()]);
            }
            $account_result = $this->auth->accountLogin($username, $password, $keeplogin ? 0 : (86400 * 15));

            // 账号登录出错
            if (!$account_result){
                $msg = $this->auth->getError();
                $msg = $msg ? $msg : __('Username or password is incorrect');
                $this->error(__($msg), $url, ['token' => $this->request->token()]);
            }
            $account = Session::get('account');

            // 如果只有一个企业，直接登录admin
            $admin_id = model('admin')->where('account_id', '=', $account['id'])->where('status', '=', 'normal')->where('usertype', '=', 0)->field('id')->find()['id'];

            if (!$admin_id){
                $msg = $this->auth->getError();
                $msg = $msg ? $msg : __('Username or password is incorrect');
                $this->error(__($msg), $url, ['token' => $this->request->token()]);
            }

            AdminLog::setTitle(__('Login'));

            $result = $this->auth->login($username, $password, $admin_id, $keeplogin ? (86400 * 15) : 0);       //主表登录

            if ($result === true) {
                Hook::listen("admin_login_after", $this->request);
                // $this->success(__('Login successful'), '/conference1/joinmeeting', ['url'=>'conference1/joinmeeting', 'account'=>$account]);
                $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'avatar' => $this->auth->avatar, 'token'=>$this->auth->token]);
            } else {
                $msg = $this->auth->getError();
                $msg = $msg ? $msg : __('Username or password is incorrect');
                $this->error(__($msg), $url, ['token' => $this->request->token()]);
            }
        }

        // 根据客户端的cookie,判断是否可以自动登录
        if ($this->auth->autologin()) {
            $this->redirect($url);
        }
        $background = Config::get('fastadmin.login_background');
        $background = stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background;
        $this->view->assign('background', $background);
        $this->view->assign('title', __('SIGN IN'));

        $langset_name = $this->request->langset() ? $this->request->langset() : 'en';
        $this->view->assign('country_code', config('countrycode')[$langset_name]);

        Hook::listen("admin_login_init", $this->request);
        return $this->view->fetch(Config::get('tplset.login'));
    }

    /**
     * 管理员登录-old
     */
    public function loginold()
    {
        $url = $this->request->get('url', 'index/index');
        // $url = substr($url,0,strpos($url, '?')).'?ref=addtabs';
        if ($this->auth->isLogin()) {
            $this->success(__("You've logged in, do not login again"), $url, ['url'=>$url]);
        }
        if ($this->request->isPost()) {
            $username = $this->request->post('username');
            $password = $this->request->post('password');
            $keeplogin = $this->request->post('keeplogin');
            $token = $this->request->post('__token__');
            $go_type = $this->request->post('go_type');
            $rule = [
                'username'  => 'require|length:3,20',
                'password'  => 'require|length:3,30',
                '__token__' => 'token',
            ];
            $data = [
                'username'  => $username,
                'password'  => $password,
                '__token__' => $token,
            ];

            $msg = [
                'username.require'   => '账号不能为空',
                'username.length'    => '账号必须3-20个字符',
            ];

            //  登录是否开启图片验证码
            if (Config::get('fastadmin.login_captcha')) {
                $rule['captcha'] = 'require|captcha';
                $data['captcha'] = $this->request->post('captcha');
                $validate = new Validate($rule, $msg, ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
            }else{
                $validate = new Validate($rule, $msg, ['username' => __('Username'), 'password' => __('Password')]);
            }

            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()), $url, ['token' => $this->request->token()]);
            }
            $account_result = $this->auth->accountLogin($username, $password, $keeplogin ? 0 : (86400 * 15));
            // 账号登录出错
            if (!$account_result){
                $msg = $this->auth->getError();
                $msg = $msg ? $msg : __('Username or password is incorrect');
                $this->error(__($msg), $url, ['token' => $this->request->token()]);
            }
            $account = Session::get('account');
            $system_count = model('admin')->where('account_id', '=', $account['id'])->where('status', '=', 'normal')->where('usertype', '=', 1)->count();
            $company_count = model('admin')->where('account_id', '=', $account['id'])->where('status', '=', 'normal')->where('usertype', '=', 0)->count();

            if (!$go_type) { // 当未确定要进入的平台
                // 如果系统账号和企业账号都存在
                if ($system_count && $company_count) {
                    $this->success(__('选择您要进入的平台'), '/index/login', ['url'=>'index/login', 'account'=>$account, 'type'=>'choose_login']);
                }

                // 如果系统账号存在且企业账号不存在
                if ($system_count && !$company_count) {
                    $admin_id = model('admin')->where('account_id', '=', $account['id'])->where('status', '=', 'normal')->where('usertype', '=', 1)->field('id')->find()['id'];
                }
                // 如果系统账号不存在
                if (!$system_count) {
                    if (!$company_count){
                        $check_count = model('admin')->where('account_id', '=', $account['id'])->where('status', '=', 'deldete')->where('usertype', '=', 0)->count();
                        if (!$check_count) {
                            $this->error(__('您当前账号没有关联任何企业，您可以前往注册页面进行注册'), $url, ['token' => $this->request->token()]);
                        }else{
                            $this->error(__('您当前账号已被 '.$check_count.' 家企业停用，您可以联系企业管理者启用或前往注册页面进行注册'), $url, ['token' => $this->request->token()]);
                        }
                    }
                    if ($company_count > 1){
                        if ($url != 'index/index') {
                            $this->success(__('选择单位'), '/company/change', ['url'=>'company/change?url='.$url, 'account'=>$account]);
                        }else{
                            $this->success(__('选择单位'), '/company/change', ['url'=>'company/change', 'account'=>$account]);
                        }
                    }
                    // 如果只有一个公司，直接登录admin，清除account的session
                    $admin_id = model('admin')->where('account_id', '=', $account['id'])->where('status', '=', 'normal')->where('usertype', '=', 0)->field('id')->find()['id'];
                }
            }else{
                if ($go_type == 'system') {
                    if (!$system_count) {
                        $this->error(__('Illegal login'), $url, ['token' => $this->request->token()]);
                    }else{
                        $admin_id = model('admin')->where('account_id', '=', $account['id'])->where('status', '=', 'normal')->where('usertype', '=', 1)->field('id')->find()['id'];
                    }
                }elseif ($go_type == 'company') {
                    if (!$company_count) {
                        $this->error(__('Illegal login'), $url, ['token' => $this->request->token()]);
                    }else{
                        if ($company_count > 1){
                            if ($url != 'index/index') {
                                $this->success(__('选择单位'), '/company/change', ['url'=>'company/change?url='.$url, 'account'=>$account]);
                            }else{
                                $this->success(__('选择单位'), '/company/change', ['url'=>'company/change', 'account'=>$account]);
                            }
                        }
                        // 如果只有一个公司，直接登录admin，清除account的session
                        $admin_id = model('admin')->where('account_id', '=', $account['id'])->where('status', '=', 'normal')->where('usertype', '=', 0)->field('id')->find()['id'];
                    }
                }else{
                    $this->error(__('Illegal login'), $url, ['token' => $this->request->token()]);
                }
            }

            AdminLog::setTitle(__('Login'));
            $result = $this->auth->login($username, $password, $admin_id, $keeplogin ? (86400 * 15) : 0);
            if ($result === true) {
                Hook::listen("admin_login_after", $this->request);
                $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'avatar' => $this->auth->avatar, 'token'=>$this->auth->token]);
            } else {
                $msg = $this->auth->getError();
                $msg = $msg ? $msg : __('Username or password is incorrect');
                $this->error(__($msg), $url, ['token' => $this->request->token()]);
            }
        }

        // 根据客户端的cookie,判断是否可以自动登录
        if ($this->auth->autologin()) {
            $this->redirect($url);
        }
        $background = Config::get('fastadmin.login_background');
        $background = stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background;
        $this->view->assign('background', $background);
        $this->view->assign('title', __('Login'));
        Hook::listen("admin_login_init", $this->request);
        return $this->view->fetch('login_bak');
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        //$admin = Admin::get($this->auth->id);
        $admin = Admin::get(['id'=>$this->auth->id]);
        $admin->prevtime = $admin->logintime;
        $admin->save();
        $this->auth->logout();
        Hook::listen("admin_logout_after", $this->request);
        $this->success(__('Logout successful'), 'index/login');
    }


    /**
     * 注册
     */
//    public function register()
//    {
//        $url = $this->request->request('url', 'index/index');
//        if ($this->auth->id) {
//            $this->success(__('You\'ve logged in, do not register again'), $url);
//        }
//        if ($this->request->isPost()) {
////            $username = $this->request->post('username');
//            $nickname = $this->request->post('nickname');
//            $password = $this->request->post('password');
//            $mobile = $this->request->post('mobile', '');
//            $captcha = $this->request->post('captcha', '');
//            $token = $this->request->post('__token__');
//            $rule = [
////                'username'  => 'require|length:3,30|unique:admin',
//                'nickname'  => 'require|length:0,20',
//                'password'  => 'require|length:6,18',
//                'mobile'    => 'require|regex:/^1\d{10}$/',
//                'captcha'   => 'require|regex:/^\d{4}$/',
//                '__token__' => 'token',
//            ];
//            $msg = [
////                'username.require' => 'Username can not be empty',
////                'username.length'  => 'Username must be 3 to 30 characters',
////                'user.unique'      => 'Username already exist',
//                'nickname.require' => 'nickrname can not be empty',
//                'nickname.length'  => 'nickname must be 3 to 20 characters',
//                'password.require' => 'Password can not be empty',
//                'password.length'  => 'Sorry, password length must be 6 or more bits and less than 18 bits',
//                'captcha.require'  => 'Captcha can not be empty',
//                'captcha.regex'    => 'Captcha must be 4 numbers',
//                'mobile.regex'     => 'Mobile is incorrect',
//                'mobile.require'   => 'Mobile can not be empty',
//            ];
//            $data = [
////                'username'  => $username,
//                'nickname'  => $nickname,
//                'password'  => $password,
//                'mobile'    => $mobile,
//                'captcha'   => $captcha,
//                '__token__' => $token,
//            ];
//
//            $validate = new Validate($rule, $msg);
//            $result = $validate->check($data);
//            if (!$result) {
//                $this->error(__($validate->getError()), $url, ['token' => $this->request->token()]);
//            }
//            if (strlen(str_replace(' ', '', $password)) != strlen($password)) {
//                $this->error(__('抱歉，不建议使用包含空格的密码'));
//            }
//            // 校验验证码
//            $captcha_result = Sms::check($mobile, $captcha, 'register');
//            if(!$captcha_result){
//                $this->error(__('Captcha is incorrect'));
//            }
//            // 处理数据
//            $params = $this->request->param();
//            $params['salt'] = Random::alnum();
//            $params['password'] = md5(md5($params['password']) . $params['salt']);
//            $params['createtime'] = time(); //创建时间为当前时间
//            $params['updatetime'] = time(); //更新时间为当前时间
//            $params['serial'] = Callfunc::makeUuid();
//            $params['avatar'] = config('default_avatar');
//            $params['joinip'] = $this->request->header("host");
//            $params['loginip'] = $this->request->header("host");
//            $params['token'] = $params['__token__'];
//            $params['username'] = $params['mobile'];
//
//            // 1、判断账号表是否存在数据
//            $account = Db::name('account')->where('mobile', '=', $mobile)->find();
//            if ($account){
//                if (Config::get('apiSwitches.im')){
//                    if ($account['TLS']!=1 | $account['TLS'] === NULL){
//                        // 将新注册的账号添加到云通信TLS系统中
//                        $TLSapi = new TLSSigAPI();
//                        $tls_result = $TLSapi->accountImport($params['username']);
//                        if ($tls_result){
//                            $TLS = 1;
//                        }else{
//                            $TLS = 0;
//                        }
//                        Db::name('account')->where('username', '=', $mobile)->update(['TLS'=>$TLS]);
//                    }
//                }
//
//
//                // 2、判断admin用户是否有举办人角色
//                $admin_count = Db::name('auth_group_access_zoom')->alias('aga')
//                    ->join('admin a', 'aga.uid=a.id', 'LEFT')
//                    ->where('a.account_id', '=', $account['id'])
//                    ->where('aga.group_id', '=', 2)
//                    ->count();
//
//                if ($admin_count){
//                    $this->error(__('手机号已存在'), 'admin/index/login', ['token' => $this->request->token()]);
//                }
//                // 已最新的密码为准
//                Db::name('account')->where('id', '=', $account['id'])->update(['password'=>$params['password'], 'salt'=>$params['salt']]);
//
//                $account_id = $account['id'];
//            }else{
//                //2、创建账号
//                $account_field = Db::name('account')->getTableFields();
//                $account_params = array_filter(array_intersect_key($params, array_flip($account_field)));
//
//                $account_id = Db::name('account')->insertGetId($account_params);
//                if (!$account_id){
//                    $this->error(__('注册失败，请重试'), null, ['token' => $this->request->token()]);
//                }
//            }
//
//            // 开启事务
//            Db::startTrans();
//            try{
//                $params['serial'] = Callfunc::makeUuid();
//                //3、创建admin用户
//                // 获取admin表的字段名称
//                $admin_field = Db::name('admin')->getTableFields();
//                $admin_params = array_filter(array_intersect_key($params, array_flip($admin_field)));
//                $admin_params['account_id'] = $account_id;
//
//                $admin = new Admin();
//                $result_admin = $admin->save($admin_params);
//                if (!$result_admin){
//                    $this->error('注册失败，请重试', null, ['token' => $this->request->token()]);
//                }
//
//
//
//                //5、创建个人会议室
//                $conf_params = array(
//                    'name' => $admin['nickname'].__('Personal meeting room'),
//                    'serial' => Callfunc::makeUuid(),
//                    'detail' => __('用户的个人会议室'),
//                    'startTime' => date('Y-m-d h:i:s', time()),
//                    'endTime' => '2300-01-01 00:00:00',
//                    'repeat' => 'n',
//                    'meetingType' => '0',
//                    'admin_id' => $admin['id'],
//                    'status'   => '0',
//                    'option'  => '[""]',
//                    //'company_id' => $company_id,
//                    'company_id' => '',
//                    'isself'     => 1
//                );
//                $conf = new \app\admin\model\Activity_conf();
//                $conf_result = $conf->save($conf_params);
//                if ($conf_result === false) {
//                    $this->error(model('activity_conf')->getError());
//                }
//                // 6、用户关联拥有者角色、新公司、个人会议室
//                // 根据是否是邀请注册，区分对应角色组
//                $group_id =  AuthGroupAccessZoom::SPONSOR;//举办人权限
//                $aga_result = AuthGroupAccessZoom::create(['uid'=>$admin['id'], 'group_id'=>$group_id]);
//                if ($aga_result === false) {
//                    $this->error(model('AuthGroupAccessZoom')->getError());
//                }
//
//                $admin->selfConf = $conf->serial;
//                $admin->save();
//                Db::commit();
//            }catch (\Exception $e){
//                // 回滚事务
//                Db::rollback();
//                $this->error($e->getMessage());
//            }
//
//            // 7、注册成功进行登录
//            AdminLog::setTitle(__('Login'));
//            $result = $this->auth->login($mobile, $password, $admin->id, (86400 * 15));
//            if ($result === true) {
//                Hook::listen("admin_login_after", $this->request);
//                $this->success(__('Sign up successful'), 'company/perfect', ['url' => $url, 'id' => $this->auth->id, 'mobile' => $mobile, 'avatar' => $this->auth->avatar, 'token'=>$this->auth->token]);
//            }else{
//                $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
//            }
//
//        }
//        //判断来源
//        $referer = $this->request->server('HTTP_REFERER');
//        if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
//            && !preg_match("/(admin\/index\/login|admin\/index\/register)/i", $referer)) {
//            $url = $referer;
//        }
//        $this->view->assign('url', $url);
//        $background = Config::get('fastadmin.login_background');
//        $background = stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background;
//        $this->view->assign('background', $background);
////        if ($company_serial) {
////            $this->view->assign('title', __('加入邀请'));
////        }else{
//            $this->view->assign('title', __("SIGN UP, IT'S FREE"));
//        //}
//
//        $langset_name = $this->request->langset() ? $this->request->langset() : 'en';
//        $this->view->assign('country_code', config('countrycode')[$langset_name]);
//
//        Hook::listen("admin_register_init", $this->request);
//        return $this->view->fetch();
//    }

    /***
     * 找回密码
     */
    public function findpwd(){
        if ($this->request->isPost()){
            $params = $this->request->param();
            $new_password = $params['password'];
            $confirm_password = $params['confirm_password'];
            // 校验
            if (!$params['mobile']) {
                $this->error(__('Please enter your mobile'));
            }
            $account_info = Db::name('account')->where(['username'=>$params['mobile']])->field('id')->find();
            if (!$account_info) {
                $this->error(__('This number is not registered'));
            }
            if (!$new_password) {
                $this->error(__('Please enter a new password'));
            }
            if (!$confirm_password) {
                $this->error(__('Please enter the confirmation password'));
            }
            if (strlen(str_replace(' ', '', $new_password)) != strlen($new_password)) {
                $this->error(__('Sorry, the password cannot contain Spaces'));
            }
            if ((strlen($new_password) < 6) || strlen($new_password) > 17) {
                $this->error(__('Sorry, password length must be 6 or more bits and less than 18 bits'));
            }
            if ($new_password != $confirm_password) {
                $this->error(__('The passwords are different'));
            }

            // 校验验证码
            $captcha_result = Sms::check($params['mobile'], $params['captcha'], 'resetpwd');
            if(!$captcha_result){
                $this->error(__('Captcha is incorrect'));
            }
            $params['salt'] = Random::alnum();
            $params['password'] = md5(md5($params['password']) . $params['salt']);
            $account_field = Db::name('admin')->getTableFields();
            $account_params = array_filter(array_intersect_key($params, array_flip($account_field)));
            $account_result = Db::name('account')->where('mobile', '=', $params['mobile'])->update($account_params);
            if (!$account_result){
                $this->error(__('重置密码失败，请重试'));
            }
            $admin_field = Db::name('admin')->getTableFields();
            $admin_params = array_filter(array_intersect_key($params, array_flip($admin_field)));
            $admin_result = Db::name('admin')->where('mobile', '=', $params['mobile'])->update($admin_params);
            $this->success(__('重置密码成功，即将跳转...'), 'admin/index/login');
        }

        $background = Config::get('fastadmin.login_background');
        $background = stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background;
        $this->view->assign('background', $background);
        $this->view->assign('title', __('Retrieve password'));

        $langset_name = $this->request->langset() ? $this->request->langset() : 'en';
        $this->view->assign('country_code', config('countrycode')[$langset_name]);

        return $this->view->fetch();
    }

    /*
     * 推广首页
    */
    public function home(){
        $request = Request::instance();
        $domain = $request->domain();

        if (strpos($domain, 'umedoctor.com') !== false) {
            $title = '';
            $template = 'home_umedoctor';
        }elseif (strpos($domain, 'duokelai.com') !== false) {
            $title = '';
            $template = 'home_duokelai';
        }elseif (strpos($domain, 'vytalk.com') !== false) {
            $title = '';
            $template = 'home';
        }else{
            $title = __('Home');
            $template = config('tplset.home');
        }

        $this->view->assign('title', $title);
        if (config('tplset.home') == 'home_hide') {
            $this->view->assign('nowfunction', 'browser_msg');
        }else{
            $this->view->assign('nowfunction', 'home');
        }
        if(config('web_platform') == 'shangkebao'){
            Log::error('tttttttttt');
            $this->redirect('/skb/index.html');
        }
        $this->view->engine->layout(false); // 临时关闭当前模板的布局功能，将无法使用框架的js
        return $this->view->fetch($template);
    }

    /***
     * 文档展现
     */
    public function about(){
        $type = $this->request->request('t');
        $hide = $this->request->request('h');
        $appname = $this->request->request('appname');

        switch ($type) {
            case 'privacy':
                $title = 'Privacy Policy';
                if ($appname != '') {
                    $template = 'privacy_'.$appname;
                }else{
                    $template = 'privacy_'.config('web_platform');
                }
                break;
            case 'terms':
                $title = 'Terms of Service';
                if ($appname != '') {
                    $template = 'terms_'.$appname;
                }else{
                    $template = 'terms_'.config('web_platform');
                }
                break;
            case 'pricing':
                $title = 'Plans & Pricing';
                $template = 'pay_plan';
                break;
            case 'contact_sales':
                $title = 'CONTACT SALES ';
                $template = config('tplset.contact_sales'); //'contact_us';

                $contact_industry = Db::name('contact_industry')->where(['status'=>1])->field('id,name')->select();
                $this->view->assign('contact_industry', $contact_industry);

                $contact_sales_model = new \app\admin\model\ContactSales();
                $unit_scale = $contact_sales_model->getUnitScale();
                $this->view->assign('unit_scale', $unit_scale);

                $langset_name = $this->request->langset() ? $this->request->langset() : 'en';
                $this->view->assign('country_code', config('countrycode')[$langset_name]);
                break;
            case 'browser_msg':
                $title = 'Warning';
                $template = 'browser_msg';
                $this->view->assign('nowfunction', 'browser_msg');
                break;
            case 'payplan':
                $title = '计划和定价';
                $template = 'pay_plan';
                break;
            default:
                $title = 'Privacy Policy';
                $template = 'privacy';
                break;
        }

        $this->view->assign('hide', $hide);
        $this->view->assign('title', __($title));
        $this->view->assign('appname', $appname);
        $this->view->engine->layout(false); // 临时关闭当前模板的布局功能，将无法使用框架的js
        return $this->view->fetch($template);
    }

    /***
     * 二级页展现
     */
    public function page(){
        $type = $this->request->request('t');
        $hide = $this->request->request('h');

        switch ($type) {
            case 'meeting':
                $title = 'Meeting';
                $template = 'meeting';
                break;
            case 'webinar':
                $title = 'Webinar';
                $template = 'webinar';
                break;
            case 'terminal':
                $title = 'Intelligent video terminal';
                $template = 'terminal';
                break;
            case 'ehs':
                $title = 'EHS field operations';
                $template = 'ehs';
                break;
            default:
                $title = 'Meeting';
                $template = 'meeting';
                break;
        }

        $this->view->assign('hide', $hide);
        $this->view->assign('title', __($title));
        $this->view->engine->layout(false); // 临时关闭当前模板的布局功能，将无法使用框架的js
        return $this->view->fetch($template);
    }

    /*
     * 变更语言
     */
    public function changelanguage(){
        $this->view->engine->layout(false); // 临时关闭当前模板的布局功能，将无法使用框架的js
        return $this->view->fetch();
    }


    public function company(){

        return $this->view->fetch();
    }

    //调取图片验证码
    public function getCaptcha()
    {
        $captchaConf = Config::get('captcha');
        $captcha = new Captcha($captchaConf);
        return $captcha->entry(1);
    }


}
